<template>
  <div class='container'>
    <el-form :rules='addRules'
             ref="formInfo"
             :model="formInfo"
             label-width="80px">
      <el-form-item label="文章标题"
                    prop="addValueText">
        <el-input v-model="formInfo.addValueText"
                  placeholder="请输入文章标签"></el-input>
      </el-form-item>
      <el-form-item label="文章内容"
                    prop="contentText">
        <quill-editor v-model="formInfo.contentText"
                      class="fwb">
        </quill-editor>
      </el-form-item>
      <span slot="footer"
            class="dialog-footer">
      </span><br><br><br><br><br>
      <el-form-item label="视频地址"
                    prop="video">
        <el-input v-model="formInfo.video"
                  placeholder="请输入视频地址"></el-input>
      </el-form-item>
      <div class="buttonRight">
        <el-button @click="cleanAdd">取 消</el-button>
        <el-button type="primary"
                   @click="submit('formInfo')">确 定</el-button>
      </div>
    </el-form>
    <br><br><br>
  </div>
</template>

<script>
import { update, add } from '@/api/hmmm/articles'
import { quillEditor } from 'vue-quill-editor' //调用编辑器
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
export default {
  components: {
    quillEditor
  },
  props: {
    idx: {
      type: [Object],
      require: true
    }
  },
  data() {
    return {
      addRules: {
        addValueText: [{ required: true, message: '请输入文章标题', trigger: 'blur' }],
        contentText: [{ required: true, message: '请输入文章内容', trigger: 'blur' }]
      },
      formInfo: {
        contentText: '',
        addValueText: '',
        video: ''
      }
    }
  },
  created() {
    this.$nextTick(() => {
      if (this.idx) {
        // console.log(this.idx)
        this.formInfo.contentText = this.idx.articleBody
        // console.log(this.formInfo.contentText)
        this.formInfo.addValueText = this.idx.title
        // console.log(this.formInfo.addValueText)
        this.formInfo.video = this.idx.videoURL
        // console.log(this.formInfo.video)
      }
    })
  },
  methods: {
    submit(form) {
      this.$refs[form].validate(async (valid) => {
        if (valid) {
          try {
            if (this.idx) {
              await update({
                title: this.formInfo.addValueText,
                articleBody: this.formInfo.contentText,
                videoURL: this.formInfo.video,
                id: this.idx.id
              })
            } else {
              await add({
                title: this.formInfo.addValueText,
                articleBody: this.formInfo.contentText,
                videoURL: this.formInfo.video
              })
            }
            this.contentText = ''
            this.addValueText = ''
            this.video = ''
            this.$message.success('操作成功')
          } catch (error) {
            console.log(error)
            this.$message.error('操作失败')
          }
          this.$emit('close')
          this.$emit('resetGetList')
          this.$emit('resetEditGetList')
        } else {
          this.$message.error('请输入有效值')
        }
      })
    },
    cleanAdd() {
      this.$emit('close')
      this.formInfo.contentText = ''
      this.formInfo.addValueText = ''
    }
  }
}
</script>

<style lang="less" scoped>
.fwb {
  height: 290px;
}
.buttonRight {
  position: absolute;
  right: 20px;
  bottom: 20px;
}
</style>